package com.study.demo.controller;

import com.study.security.app.TokenStoreConfig;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * @author LiYongKui
 * @version 1.0
 * @date 2021/3/31 10:54
 */
@RestController
public class TestController {

    @GetMapping("/test")
    public String test(){
        return "success";
    }

    @Autowired
    private ProviderSignInUtils providerSignInUtils;

    @PostMapping("/sign-up")
    public void signUp(String username, HttpServletRequest request){

        //第三方登陆后注册 或者绑定时操作
        providerSignInUtils.doPostSignUp(username,new ServletWebRequest(request));
    }

    @GetMapping("/me")
    public Object getCurrentUser(UserDetails user,HttpServletRequest request) throws Exception{
        String header = request.getHeader("Authorization");
        String token = StringUtils.substringAfter(header, "bearer ");

        Claims claims = Jwts.parser().setSigningKey("".getBytes("UTF-8"))
                .parseClaimsJws(token).getBody();

        String company = (String) claims.get("company");
        System.out.println("jwt 自定义属性："+company);
        return user;
    }
}
